﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using GA.Server.ApiInfo;

namespace GA.Server.Api
{
    [Serializable]
    public class ApiInfoPlayer : SignedApiCall
    {
        public ApiPlayer ApiPlayer { get; private set; }
        public IEnumerable<ApiCharacter> ApiCharacters { get; private set; }

        public ApiInfoPlayer()
        {
        }

        public ApiInfoPlayer(int clientId, string authorizationText, string sessionId)
            : base(clientId, authorizationText, sessionId)
        {
        }

        protected override string ApiCallName
        {
            get { return "Player"; }
        }

        public void LoadApi(string playerName)
        {
            LoadSignedApiCall(string.Format("u=" + playerName));
            XmlNodeList xmlNodeList = XmlDocument.GetElementsByTagName("a:get_player_stats_headerbyplayerResult");
            if (xmlNodeList.Count > 0)
            {
                ApiPlayer = new ApiPlayer(xmlNodeList[0].CreateNavigator().ReadSubtree());
                
                XmlNodeList characterNodes = XmlDocument.GetElementsByTagName("a:get_player_characters_by_playerResult");
                if (characterNodes.Count > 0)
                {
                    List<ApiCharacter> characters = new List<ApiCharacter>();
                    foreach (XmlNode characterNode in characterNodes)
                    {
                        characters.Add(new ApiCharacter(characterNode.CreateNavigator().ReadSubtree()));
                    }
                    ApiCharacters = characters;
                }
            }
        }

    }
}
